package com.hxc.web.controller.interest;

import java.util.List;

import com.hxc.common.constant.HttpStatus;
import com.hxc.interest.domain.TNoticeRead;
import com.hxc.common.core.domain.entity.SysUser;
import com.hxc.common.core.domain.model.LoginUser;
import com.hxc.common.utils.ServletUtils;
import com.hxc.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hxc.common.annotation.Log;
import com.hxc.common.core.controller.BaseController;
import com.hxc.common.core.domain.AjaxResult;
import com.hxc.common.enums.BusinessType;
import com.hxc.interest.domain.TNoticeInfo;
import com.hxc.interest.service.ITNoticeInfoService;
import com.hxc.common.utils.poi.ExcelUtil;
import com.hxc.common.core.page.TableDataInfo;

/**
 * 通知公告信息Controller
 * 
 * @author hxc
 * @date 2021-08-16
 */
@Api(tags = "乡村公告管理")
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/interest/notice")
public class TNoticeInfoController extends BaseController
{
    @Autowired
    private ITNoticeInfoService tNoticeInfoService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询通知公告信息列表
     */
    @PreAuthorize("@ss.hasPermi('interest:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(TNoticeInfo tNoticeInfo)
    {
        startPage();
        List<TNoticeInfo> list = tNoticeInfoService.selectTNoticeInfoList(tNoticeInfo);
        return getDataTable(list);
    }

    /**
     * 导出通知公告信息列表
     */
    @PreAuthorize("@ss.hasPermi('interest:notice:export')")
    @Log(title = "通知公告信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TNoticeInfo tNoticeInfo)
    {
        List<TNoticeInfo> list = tNoticeInfoService.selectTNoticeInfoList(tNoticeInfo);
        ExcelUtil<TNoticeInfo> util = new ExcelUtil<TNoticeInfo>(TNoticeInfo.class);
        return util.exportExcel(list, "通知公告信息数据");
    }

    /**
     * 获取通知公告信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('interest:notice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tNoticeInfoService.selectTNoticeInfoById(id));
    }

    /**
     * 新增通知公告信息
     */
    @PreAuthorize("@ss.hasPermi('interest:notice:add')")
    @Log(title = "通知公告信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TNoticeInfo tNoticeInfo)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        tNoticeInfo.setUserId(user.getUserId());
        return toAjax(tNoticeInfoService.insertTNoticeInfo(tNoticeInfo));
    }

    /**
     * 修改通知公告信息
     */
    @PreAuthorize("@ss.hasPermi('interest:notice:edit')")
    @Log(title = "通知公告信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TNoticeInfo tNoticeInfo)
    {
        return toAjax(tNoticeInfoService.updateTNoticeInfo(tNoticeInfo));
    }

    /**
     * 删除通知公告信息
     */
    @PreAuthorize("@ss.hasPermi('interest:notice:remove')")
    @Log(title = "通知公告信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tNoticeInfoService.deleteTNoticeInfoByIds(ids));
    }

    /**
     * 查询公告信息
     */
    @ApiOperation(value = "查询公告信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", value = "公告id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long")
    })
    @Log(title = "查询公告信息", businessType = BusinessType.OTHER)
	@GetMapping("/queryReadNotice")
    public AjaxResult queryReadNotice(TNoticeRead noticeRead)
    {
        if(noticeRead.getNoticeId()== null || noticeRead.getUserId() == null){
            return AjaxResult.error(HttpStatus.BAD_REQUEST,"请求参数不合法");
        }
        //修改阅读量
        tNoticeInfoService.editReadNum(noticeRead);
        return AjaxResult.success(tNoticeInfoService.selectTNoticeInfoById(noticeRead.getNoticeId()));
    }

    /**
     * 查询公告和通知信息
     */
    @ApiOperation(value = "查询公告和通知信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeTypeEums", value = "公告类型(1:通知，2:公告)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "sunshineType", value = "阳光三务类型 (1:党务，2:村务，3:财务)", dataType = "Integer")
    })
    @Log(title = "查询公告和通知信息", businessType = BusinessType.OTHER)
    @GetMapping("/queryNitoceList")
    public AjaxResult queryNitoceList(TNoticeInfo tNoticeInfo)
    {
        if(tNoticeInfo.getUserId() == null){
            return AjaxResult.error(HttpStatus.BAD_REQUEST,"请求参数不合法");
        }
        List<TNoticeInfo> list = tNoticeInfoService.queryNitoceList(tNoticeInfo);
        return AjaxResult.success(list);
    }

    /**
     * 查询公告和通知信息
     */
    @ApiOperation(value = "查询我的通知公告(个人中心)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeType", value = "公告类型(1:通知，2:公告)", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "userId", value = "用户id",required = true, dataType = "Long")
    })
    @Log(title = "查询我的通知公告", businessType = BusinessType.OTHER)
    @GetMapping("/queryMineNitoceList")
    public AjaxResult queryMineNitoceList(TNoticeInfo tNoticeInfo)
    {
        List<TNoticeInfo> list = tNoticeInfoService.queryMineNitoceList(tNoticeInfo);
        return AjaxResult.success(list);
    }

}
